package models

import (
	"time"

	"gorm.io/gorm"
)

// BaseModel 基础模型，包含审计字段
type BaseModel struct {
	ID       uint           `gorm:"primarykey" json:"id" example:"1"`
	CreateAt time.Time      `gorm:"column:create_at" json:"createAt" example:"2023-01-01T00:00:00Z"`
	UpdateAt time.Time      `gorm:"column:update_at" json:"updateAt" example:"2023-01-01T00:00:00Z"`
	DeleteAt gorm.DeletedAt `gorm:"column:delete_at;index" json:"deleteAt" swaggertype:"primitive,string" example:"null"`
	CreateBy uint           `gorm:"column:create_by" json:"createBy" example:"1"`
	UpdateBy uint           `gorm:"column:update_by" json:"updateBy" example:"1"`
	DeleteBy uint           `gorm:"column:delete_by" json:"deleteBy" example:"0"`
}

// TenantModel 租户基础模型，包含租户ID
type TenantModel struct {
	BaseModel
	TenantID uint `gorm:"index;not null" json:"tenantId"`
}

// BeforeCreate 创建前设置创建时间
func (m *BaseModel) BeforeCreate(tx *gorm.DB) error {
	m.CreateAt = time.Now()
	m.UpdateAt = time.Now()
	return nil
}

// BeforeUpdate 更新前设置更新时间
func (m *BaseModel) BeforeUpdate(tx *gorm.DB) error {
	m.UpdateAt = time.Now()
	return nil
}
